package com.minimalist.lindi.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import cn.hutool.core.lang.Assert;
import com.minimalist.common.config.exception.BusinessException;
import com.minimalist.common.config.mybatis.bo.PageResp;
import com.minimalist.common.entity.lindi.po.LindiSubsidyApplication;
import com.minimalist.common.entity.lindi.vo.enterpriseaccount.LindiEnterpriseAccountVO;
import com.minimalist.common.entity.lindi.vo.subsidyapplication.LindiSubsidyApplicationQueryVO;
import com.minimalist.common.entity.lindi.vo.subsidyapplication.LindiSubsidyApplicationTownItemVO;
import com.minimalist.common.entity.lindi.vo.subsidyapplication.LindiSubsidyApplicationVO;
import com.minimalist.common.entity.lindi.vo.subsidyapplicationfile.LindiSubsidyApplicationFileVO;
import com.minimalist.common.entity.lindi.vo.subsidyapplicationtownpayment.LindiSubsidyApplicationTownPaymentVO;
import com.minimalist.common.feign.client.basic.FileFeignClient;
import com.minimalist.common.response.R;
import com.minimalist.common.utils.UnqIdUtil;
import com.minimalist.lindi.manager.LindiSubsidyApplicationManager;
import com.minimalist.lindi.mapper.LindiSubsidyApplicationMapper;
import com.minimalist.lindi.service.LindiSubsidyApplicationService;
import com.minimalist.lindi.service.LindiSubsidyApplicationTownItemService;
import com.minimalist.lindi.service.LindiSubsidyApplicationTownPaymentService;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.spring.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 林地指标交易平台_补贴申请表 服务层实现。
 *
 * @author 11987
 * @since 2025-06-09
 */
@Service
public class LindiSubsidyApplicationServiceImpl extends ServiceImpl<LindiSubsidyApplicationMapper, LindiSubsidyApplication> implements LindiSubsidyApplicationService{

    @Autowired
    private LindiSubsidyApplicationMapper mapper;

    @Autowired
    private LindiSubsidyApplicationManager lindiSubsidyApplicationManager;

    @Autowired
    private FileFeignClient fileFeignClient;

    @Autowired
    private LindiSubsidyApplicationTownPaymentService lindiSubsidyApplicationTownPaymentService;

    @Autowired
    private LindiSubsidyApplicationTownItemService lindiSubsidyApplicationTownItemService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void addSubsidyApplication(LindiSubsidyApplicationVO subsidyApplicationVO){
        LindiSubsidyApplication record = BeanUtil.copyProperties(subsidyApplicationVO
                , LindiSubsidyApplication.class);
        record.setSubsidyApplicationId(UnqIdUtil.uniqueId());
        record.setSubsidyStatus("1");
        int insert = mapper.insert(record, true);

        // 处理文件列表
        if (subsidyApplicationVO.getSubsidyApplicationFileList() != null){
            subsidyApplicationVO.getSubsidyApplicationFileList().forEach(
                    item ->
                            item.setSubsidyApplicationId(record.getSubsidyApplicationId()));
            lindiSubsidyApplicationManager.insertSubsidyApplicationFiles(
                    subsidyApplicationVO.getSubsidyApplicationFileList());
        }

        // 处理镇街支付明细列表
        if (subsidyApplicationVO.getSubsidyApplicationTownPaymentList() != null){
            subsidyApplicationVO.getSubsidyApplicationTownPaymentList().forEach(
                    item ->
                            item.setSubsidyApplicationId(record.getSubsidyApplicationId()));
            lindiSubsidyApplicationTownPaymentService.batchAddSubsidyApplicationTownPayment(
                    subsidyApplicationVO.getSubsidyApplicationTownPaymentList());
        }

        // 处理镇街明细列表
        if (subsidyApplicationVO.getSubsidyApplicationTownItemList() != null){
            subsidyApplicationVO.getSubsidyApplicationTownItemList().forEach(
                    item ->
                            item.setSubsidyApplicationId(record.getSubsidyApplicationId()));
            lindiSubsidyApplicationTownItemService.batchAddSubsidyApplicationTownItem(
                    subsidyApplicationVO.getSubsidyApplicationTownItemList());
        }

        Assert.isTrue(insert == 1, () -> new BusinessException(
                "添加补贴申请失败"));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteSubsidyApplicationBySubsidyApplicationId(
            Long subsidyApplicationId){
        LindiSubsidyApplication record = mapper
                .selectSubsidyApplicationBySubsidyApplicationId(subsidyApplicationId);
        Assert.notNull(record, () -> new BusinessException(
                "补贴申请不存在"));

        // 删除相关文件
        lindiSubsidyApplicationManager
                .deleteSubsidyApplicationFilesBySubsidyApplicationId(
                        subsidyApplicationId);

        // 删除相关镇街支付明细
        lindiSubsidyApplicationTownPaymentService.deleteSubsidyApplicationTownPaymentBySubsidyApplicationId(subsidyApplicationId);

        // 删除相关镇街明细
        lindiSubsidyApplicationTownItemService.deleteSubsidyApplicationTownItemBySubsidyApplicationId(subsidyApplicationId);

        int delete = mapper.delete(record);
        Assert.isTrue(delete == 1, () -> new BusinessException(
                "删除补贴申请失败"));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateSubsidyApplicationBySubsidyApplicationId(
            LindiSubsidyApplicationVO subsidyApplicationVO){
        LindiSubsidyApplication record = mapper
                .selectSubsidyApplicationBySubsidyApplicationId(subsidyApplicationVO
                        .getSubsidyApplicationId());
        Assert.notNull(record, () -> new BusinessException(
                "补贴申请不存在"));
        BeanUtil.copyProperties(subsidyApplicationVO, record
                , CopyOptions.create().ignoreNullValue());

        // 处理文件列表更新
        if (subsidyApplicationVO.getSubsidyApplicationFileList() != null){
            lindiSubsidyApplicationManager
                    .deleteSubsidyApplicationFilesBySubsidyApplicationId(
                            subsidyApplicationVO.getSubsidyApplicationId());
            lindiSubsidyApplicationManager.insertSubsidyApplicationFiles(
                    subsidyApplicationVO.getSubsidyApplicationFileList());
        }

        // 处理镇街支付明细列表更新
        if (subsidyApplicationVO.getSubsidyApplicationTownPaymentList() != null){
            // 删除现有的镇街支付明细
            lindiSubsidyApplicationTownPaymentService
                    .deleteSubsidyApplicationTownPaymentBySubsidyApplicationId(
                            subsidyApplicationVO.getSubsidyApplicationId());
            // 插入新的镇街支付明细
            subsidyApplicationVO.getSubsidyApplicationTownPaymentList().forEach(
                    item -> {
                        item.setSubsidyApplicationId(subsidyApplicationVO.getSubsidyApplicationId());
                        item.setSubsidyApplicationTownPaymentId(UnqIdUtil.uniqueId());
                    });
            lindiSubsidyApplicationTownPaymentService.batchAddSubsidyApplicationTownPayment(
                    subsidyApplicationVO.getSubsidyApplicationTownPaymentList());
        }

        // 处理镇街明细列表更新
        if (subsidyApplicationVO.getSubsidyApplicationTownItemList() != null){
            // 删除现有的镇街明细
            lindiSubsidyApplicationTownItemService
                    .deleteSubsidyApplicationTownItemBySubsidyApplicationId(
                            subsidyApplicationVO.getSubsidyApplicationId());
            // 插入新的镇街明细
            subsidyApplicationVO.getSubsidyApplicationTownItemList().forEach(
                    item ->
                            item.setSubsidyApplicationId(subsidyApplicationVO.getSubsidyApplicationId()));
            lindiSubsidyApplicationTownItemService.batchAddSubsidyApplicationTownItem(
                    subsidyApplicationVO.getSubsidyApplicationTownItemList());
        }

        int update = mapper.update(record);
        Assert.isTrue(update == 1, () -> new BusinessException(
                "修改补贴申请失败"));
    }

    @Override
    public PageResp<LindiSubsidyApplicationVO> getPageSubsidyApplicationList(
            LindiSubsidyApplicationQueryVO queryVO){
        //分页查询
        Page<LindiSubsidyApplication> recordPage = mapper
                .selectPageSubsidyApplicationList(queryVO);
        //数据转换
        List<LindiSubsidyApplicationVO> voList = BeanUtil.copyToList(
                recordPage.getRecords()
                , LindiSubsidyApplicationVO.class);
        return new PageResp<>(voList, recordPage.getTotalRow());
    }

    @Override
    public LindiSubsidyApplicationVO getSubsidyApplicationBySubsidyApplicationId(
            Long subsidyApplicationId){
        LindiSubsidyApplication record = mapper
                .selectSubsidyApplicationBySubsidyApplicationId(
                        subsidyApplicationId);
        Assert.notNull(record, () -> new BusinessException(
                "补贴申请不存在"));

        // 获取文件列表
        List<LindiSubsidyApplicationFileVO> fileList =
                lindiSubsidyApplicationManager
                        .getSubsidyApplicationFileListBySubsidyApplicationId
                                (subsidyApplicationId);

        // 获取企业账号信息
        LindiEnterpriseAccountVO enterpriseAccountVO =
                lindiSubsidyApplicationManager.getEnterpriseAccountByEnterpriseId(
                        record.getEnterpriseAccountId());

        // 获取镇街支付明细列表
        List<LindiSubsidyApplicationTownPaymentVO> townPaymentList =
                lindiSubsidyApplicationTownPaymentService
                        .getSubsidyApplicationTownPaymentListBySubsidyApplicationId(subsidyApplicationId);

        // 获取镇街明细列表
        List<LindiSubsidyApplicationTownItemVO> townItemList =
                lindiSubsidyApplicationTownItemService
                        .getSubsidyApplicationTownItemListBySubsidyApplicationId(subsidyApplicationId);

        LindiSubsidyApplicationVO lindiSubsidyApplicationVO =
                BeanUtil.copyProperties(record, LindiSubsidyApplicationVO.class);
        lindiSubsidyApplicationVO.setEnterpriseAccount(enterpriseAccountVO);
        lindiSubsidyApplicationVO.setSubsidyApplicationFileList(fileList);
        lindiSubsidyApplicationVO.setSubsidyApplicationTownPaymentList(townPaymentList);
        lindiSubsidyApplicationVO.setSubsidyApplicationTownItemList(townItemList);
        return lindiSubsidyApplicationVO;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteSubsidyApplicationFileByFileId(Long fileId){
        R<Void> result = fileFeignClient.deleteFile(fileId);
        Assert.isTrue(result.getCode() == 200, () -> new
                BusinessException("删除补贴申请文件失败"));
        lindiSubsidyApplicationManager.deleteSubsidyApplicationFilesByFileId(fileId);
    }
}